package com.ruoyi.web.controller.ted;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demooo1.domain.OneDayDivination;
import com.ruoyi.demooo1.service.IOneDayDivinationService;
import com.ruoyi.demooo1.service.OneService;
import com.ruoyi.framework.web.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 每日一卦Controller
 *
 * @author ted
 * @date 2023-08-22
 */
@RestController
@RequestMapping("/ted/divination")
public class OneDayDivinationController extends BaseController {
    @Autowired
    private IOneDayDivinationService oneDayDivinationService;
    @Autowired
    private OneService oneService;

    /**
     * 查询每日一卦列表
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:list')")
    @GetMapping("/list")
    public TableDataInfo list(OneDayDivination oneDayDivination) {
        PermissionService permissionService = new PermissionService();
        if (permissionService.hasRole("admin,sysadmin")) {
            oneDayDivination.setCreateBy("");
        } else {
            oneDayDivination.setCreateBy(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<OneDayDivination> list = oneDayDivinationService.selectOneDayDivinationList(oneDayDivination);
        return getDataTable(list);
    }

    /**
     * 导出每日一卦列表
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:export')")
    @Log(title = "每日一卦", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OneDayDivination oneDayDivination) {
        List<OneDayDivination> list = oneDayDivinationService.selectOneDayDivinationList(oneDayDivination);
        ExcelUtil<OneDayDivination> util = new ExcelUtil<OneDayDivination>(OneDayDivination.class);
        util.exportExcel(response, list, "每日一卦数据");
    }

    /**
     * 获取每日一卦详细信息
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(oneDayDivinationService.selectOneDayDivinationById(id));
    }

    /**
     * 新增每日一卦
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:add')")
    @Log(title = "每日一卦", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody OneDayDivination oneDayDivination) {
       oneService.saveOne(oneDayDivination);
    }

    /**
     * 修改每日一卦
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:edit')")
    @Log(title = "每日一卦", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OneDayDivination oneDayDivination) {
        return toAjax(oneDayDivinationService.updateOneDayDivination(oneDayDivination));
    }

    /**
     * 删除每日一卦
     */
    @PreAuthorize("@ss.hasPermi('ted:divination:remove')")
    @Log(title = "每日一卦", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oneDayDivinationService.deleteOneDayDivinationByIds(ids));
    }
}
